package at.gv.bmwf.fons.model.ontology.sources;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.google.common.collect.Lists;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class OefosSource extends FileDataSource {
	private final static String OEFOS_2002 = "http://www.statistik.at/kdb/downloads/csv/OEFOS2002_DE_CTI_20100302_133046.txt";
	private final static String OEFOS_2012 = "http://www.statistik.at/kdb/downloads/csv/OEFOS2012_DE_CTI_20121009_130721.txt";
	
	private final static String prefix = "http://www.bmwf.gv.at/fons/ontologies/oefos#";
	
	
	public OefosSource() {
		super(new File("oefos.ttl"), null);
	}
		
	@Override
	public OntModel createABox() {
		OntModel newModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
		
		try {
			newModel.addSubModel(createABox(new URL(OEFOS_2002), 2002, Lists.newArrayList(10)));
			newModel.addSubModel(createABox(new URL(OEFOS_2012), 2012, Lists.newArrayList(100, 10)));
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return newModel;
	}
	
	private OntModel createABox(URL url, int version, List<Integer> divisorsForSuperCategories) {
		OntModel newModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
		
		try {
			BufferedInputStream source = new BufferedInputStream(url.openStream());
			BufferedReader reader = new BufferedReader(new InputStreamReader(source));
		
			reader.readLine(); //ignore the first line
			String line = reader.readLine();

			while (line != null) {
				String parts[] = line.split(";");
								
				try {
					final int level = Integer.parseInt(parts[0]);
					final int code = Integer.parseInt(parts[1].substring(1, parts[1].length()-1));
					final String name = parts[3].substring(1, parts[3].length()-1);
									
					final OntProperty codeProperty = newModel.createOntProperty(prefix + "code");
					final OntProperty versionProperty = newModel.createOntProperty(prefix + "version");
					final OntProperty categoryProperty = newModel.createOntProperty(prefix + "category");
					
					final OntResource resource = newModel.createOntResource(null);
					resource.addLiteral(codeProperty, code);
					resource.addLiteral(versionProperty, version);
					resource.addLabel(name, "de");
					
					if (level > 1) {
						int superCode = code / divisorsForSuperCategories.get(level-2);
						final OntResource category = newModel.createOntResource(null);
						category.addLiteral(codeProperty, superCode);
						category.addLiteral(versionProperty, version);
						resource.addProperty(categoryProperty, category);
					}
					
				} catch (NumberFormatException exception) {
					//ignore the line
				} finally {
					line = reader.readLine();
				}
			}
			
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return newModel;
	}

}
